import os
import subprocess

def expand(var):
    p = subprocess.Popen(['kpsexpand', var], stdout = subprocess.PIPE)
    return p.communicate()[0].rstrip()

def machine_hardware():
    p = subprocess.Popen(['uname', '-m'], stdout = subprocess.PIPE)
    return p.communicate()[0].rstrip()

def hardware_platform():
    p = subprocess.Popen(['uname', '-i'], stdout = subprocess.PIPE)
    return p.communicate()[0].rstrip()

selfautoloc = expand('$SELFAUTOLOC').decode()
selfautodir = expand('$SELFAUTODIR').decode()
selfautoparent = expand('$SELFAUTOPARENT').decode()

machine = machine_hardware().decode()
platform = hardware_platform().decode()
if machine == 'x86_64':
    lib_subdir = 'lib64'
    if platform == 'AuthenticAMD':
        CFLAGS = '-g -O2 -march=k8 -pipe'
    else:
        CFLAGS = '-g -O2 -pipe'
else:
    lib_subdir = 'lib'
    CFLAGS = '-g -O2 -pipe'

CFLAGS = (os.getenv('CFLAGS') if os.getenv('CFLAGS') != None else CFLAGS)
PIC = '-fPIC'

home = os.getenv('HOME')
texmf = os.path.join(home, 'texmf')
lib_dir = os.path.join(home, lib_subdir)
include_dir = os.path.join(home, 'include')
bin_dir = os.path.join(home, 'bin')

fontforge_source_dir   = os.path.join('..', 'fontforge') 
fontforge_include_dirs = [
    os.path.join(fontforge_source_dir, 'inc'),
    os.path.join(fontforge_source_dir, 'fontforge'),
    ]

imagemagick_include_dirs = ['/usr/include/ImageMagick']

python_package_CFLAGS = CFLAGS
python_package_prefix = home

plugin_dir = os.path.join(lib_dir, 'kt', 'plugin')
ant_generic_dir = os.path.join(texmf, 'ant', 'generic')
ant_kt_dir = os.path.join(texmf, 'ant', 'kt')
python_scripts_dir = os.path.join(texmf, 'scripts', 'kt', 'python')
cmap_dir = os.path.join(texmf, 'fonts', 'cmap', 'kt')

pyver        = '3.1'
pyver_pack   = pyver.replace('.', '_')
kt_version   = '1.0'

site_packages_dir = os.path.join(lib_dir, 'python' + pyver, 'site-packages')
kt_packages_dir = os.path.join(site_packages_dir, 'kt')
egg_info_file = os.path.join(site_packages_dir, 'kt-' + kt_version + '-py' + pyver + '.egg-info')

def config_summary():
    print('Python version:        ' + pyver)
    print('OCaml binaries:        ' + bin_dir)
    print('OCaml plugins:         ' + plugin_dir)
    print('Generic ANT sources:   ' + ant_generic_dir)
    print('kt-specific sources:   ' + ant_kt_dir)
    print('Python package:        ' + kt_packages_dir)
    print('Python scripts:        ' + python_scripts_dir)
    print('CMap files:            ' + cmap_dir)
